package com.cskaoyan.controller;

import com.cskaoyan.bean.Goods;
import com.cskaoyan.bean.goods.*;
import com.cskaoyan.until.Result;
import com.cskaoyan.bean.params.BaseParam;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 商品列表及上架相关模块
 */
@Controller
@RequestMapping("admin/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    OrderService orderService;

    /**
     * 商品列表界面
     * 获取商品信息，分页显示
     * @param baseParam 分页参数
     * @param goodsListBO name（商品名模糊查询），goodsSn（商品编号），goodsId
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Result list(BaseParam baseParam, GoodsListBO goodsListBO) {

        GoodsListVO goodsListVO = goodsService.queryGoods(baseParam, goodsListBO);
        return Result.ok(goodsListVO);
    }

    /**
     * 商品上架、添加、编辑
     * 获取商品所有分类和品牌信息
     * @return
     */
    @RequestMapping("catAndBrand")
    @ResponseBody
    public Result catAndBrand() {
        CatAndBrandVO catAndBrandVO = goodsService.queryCategoryAndBrand();
        return Result.ok(catAndBrandVO);
    }

    /**
     * 商品新增的提交功能
     * @param goodsCreateBO
     * @return
     */
    @RequestMapping("create")
    @ResponseBody
    public Result create(@RequestBody GoodsCreateBO goodsCreateBO) {

        goodsService.insertGoods(goodsCreateBO);
        return Result.ok();
    }

    /**
     * 商品编辑，根据商品id获取商品信息并显示；
     * @param id
     * @return
     */
    @RequestMapping("detail")
    @ResponseBody
    public Result detail(Integer id) {
        GoodsDetailVO goodsDetail = goodsService.getGoodsDetail(id);

        return Result.ok(goodsDetail);
    }

    /**
     * 商品编辑，提交修改后的商品信息
     * @param goodsUpdateBO
     * @return
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(@RequestBody GoodsUpdateBO goodsUpdateBO) {
        goodsService.updateGoodsDetail(goodsUpdateBO);
        return Result.ok();
    }

    /**
     * 商品删除
     * @param goods
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    public Result delete(@RequestBody Goods goods) {

        goodsService.deleteGoods(goods);

        return Result.ok();
    }


}
